<?php
include("../../include/connexion.php");


function maj($mid,$mnom,$mprenom,$mdateNaiss,$msexe,$midSitu,$madresse,$mtelephone,$mmail,$midStatut,$midServ,$midPoste,$mdateEntree,$mnivEtudes) {

$req_maj = " update employe
			set idposte=$midPoste, idstat=$midStatut, idsituation=$midSitu, idservice=$midServ, nomemp=\"$mnom\", 
			prenomemp=\"$mprenom\", datenaissance=\"$mdateNaiss\", sexe=\"$msexe\", adresse=\"$madresse\", 
			tel=\"$mtelephone\", mail=\"$mmail\", dateentree=\"$mdateEntree\", niveauetudes=\"$mnivEtudes\"
			where employe.idemp = \"$mid\"";
$res_maj = mysql_query($req_maj);
return $res_maj;

}


function employe($id){
$req_employe = "Select NOMEMP AS nom,
		PRENOMEMP AS prenom,
		DATENAISSANCE AS datenaiss,
		sexe,
		typesituation,
		adresse,
		tel,
		mail,
		typestat,
		nomservice as service,
		nomposte as poste,
		dateentree,
		niveauetudes
		from employe as E left join service as S on E.idservice=S.idservice
		left join poste as P on E.idposte=P.idposte left join situationfam as SF on E.idsituation = SF.idsituation
		left join statut as ST on E.idstat= ST.idstat
		where E.idemp = \"$id\"";
$res_employe = mysql_query($req_employe);

$tab_employe;
$i = 0;
while ($employe = mysql_fetch_array($res_employe)){
	$tab_employe['nom'] = $employe['nom'];
	$tab_employe['prenom'] = $employe['prenom'];
	$tab_employe['datenaiss'] = $employe['datenaiss'];
	$tab_employe['sexe'] = $employe['sexe'];
	$tab_employe['sitfam'] = $employe['typesituation'];
	$tab_employe['adresse'] = $employe['adresse'];
	$tab_employe['tel'] = $employe['tel'];
	$tab_employe['statut'] = $employe['typestat'];
	$tab_employe['mail'] = $employe['mail'];
	$tab_employe['service'] = $employe['service'];
	$tab_employe['poste'] = $employe['poste'];
	$tab_employe['dateentree'] = $employe['dateentree'];
	$tab_employe['niveauetudes'] = $employe['niveauetudes'];
	
	$i++;
	}
return $tab_employe;
}

function query_service(){
$req_service = " Select idservice, nomService 
from service
order by nomService;";
$res_service = mysql_query($req_service);

$i=0;
$tab_serv;
while ($service = mysql_fetch_array($res_service)){
	$tab_serv[$i]['IdServ'] = $service['idservice'];
	$tab_serv[$i]['nomServ'] = $service['nomService'];
	$i++;	
}	
return $tab_serv;
}

function query_poste(){
$req_poste = " Select idposte, nomPoste 
from poste
order by nomPoste;";
$res_poste = mysql_query($req_poste);

$i=0;
$tab_poste;
while ($poste = mysql_fetch_array($res_poste)){
	$tab_poste[$i]['idPoste'] = $poste['idposte'];
	$tab_poste[$i]['nomPoste'] = $poste['nomPoste'];
	$i++;	
}	
return $tab_poste;
}

function query_situFam(){
$req_sitFam = " Select idsituation, typeSituation 
from situationfam
order by typeSituation;";
$res_sitFam = mysql_query($req_sitFam);

$i=0;
$tab_sitFam;
while ($sitFam = mysql_fetch_array($res_sitFam)){
	$tab_sitFam[$i]['idsituation'] = $sitFam['idsituation'];
	$tab_sitFam[$i]['typeSitu'] = $sitFam['typeSituation'];
	$i++;	
}	
return $tab_sitFam;
}

function query_statut(){
$req_statut = " Select idstat, typeStat
from statut
order by idStat;";
$res_statut = mysql_query($req_statut);

$i=0;
$tab_statut;
while ($statut = mysql_fetch_array($res_statut)){
	$tab_statut[$i]['idStat'] = $statut['idstat'];
	$tab_statut[$i]['statut'] = $statut['typeStat'];
	$i++;	
}	
return $tab_statut;
}


function query_comp(){
$req_comp = " Select idcomp, typeComp 
from competence
order by typecomp;";
$res_comp = mysql_query($req_comp);

$i=0;
$tab_comp;
while ($comp = mysql_fetch_array($res_comp)){
	$tab_comp[$i]['ID'] = $comp['idcomp'];	
	$tab_comp[$i]['typeComp'] = $comp['typeComp'];
	$i++;	
}	
return $tab_comp;
}


function query_resultat($nom, $prenom, $service, $poste){
$req_resultat = "Select idemp as ID,
		NOMEMP AS nom,
		PRENOMEMP AS prenom,
		DATENAISSANCE AS datenaiss,
		nomservice as service,
		nomposte as poste 
		from employe as E left join service as S on E.idservice=S.idservice
		left join poste as P on E.idposte=P.idposte";
	
$cpt =0;
 if( $nom != ""){
	if ($cpt == 0){
	$req_resultat .= " where nomEmp = \"$nom\"";
	$cpt++;
	}
	else 
	$req_resultat .= " AND nomEmp = \"$nom\"";
}
if ($prenom != ""){
	if ($cpt == 0){
	$req_resultat .= " where prenomEmp = \"$prenom\"";
	$cpt++;
	}
	else 
	$req_resultat .= " AND prenomEmp = \"$prenom\"";
}
if( strcmp($service, 'Tous') != 0){
	if($_GET['action'] == "consult_service")
		$nom_ou_id = "S.idservice";
	else
		$nom_ou_id = "nomservice";

	if ($cpt == 0){
		$req_resultat .= " where $nom_ou_id = '$service'";
		$cpt++;
	}
	else 
	$req_resultat .= " AND $nom_ou_id ='$service'";
}
if( strcmp($poste, 'Tous') != 0){
	if ($cpt == 0){
	$req_resultat .= " where nomposte = \"$poste\"";
	$cpt++;
	}
	else 
	$req_resultat .= " AND nomposte = \"$poste\"";
	}
	
	$req_resultat .= " order by nomemp;";

$rep_resultat = mysql_query($req_resultat);


$tab_employ;
$i = 0;
while ($employe = mysql_fetch_array($rep_resultat)){
	$tab_employ[$i]['ID'] = $employe['ID'];
	$tab_employ[$i]['nom'] = $employe['nom'];
	$tab_employ[$i]['prenom'] = $employe['prenom'];
	$tab_employ[$i]['datenaiss'] = $employe['datenaiss'];
	$tab_employ[$i]['service'] = $employe['service'];
	$tab_employ[$i]['poste'] = $employe['poste'];
		$i++;
}
if(isset($tab_employ)) {
	return $tab_employ;
}

}

function nouvEmploy($poste, $statut, $sitfam, $service, $nom, $prenom, $dateNaiss, $sexe,  $adresse, $telephone, $mail, $dateEntree, $nivEtudes){

if(($poste != Null) && ($statut != Null) && ($sitfam != null) && ($service != null) && ($nom!= Null) && ($prenom != Null) && ($dateEntree != null)){
$req_nouvEmpl = "Insert into `pgrh`.`employe` (`IDEMP`, `IDPOSTE`, `IDSTAT`, `IDSITUATION`, `IDSERVICE`, `NOMEMP`, `PRENOMEMP`, 
`DATENAISSANCE`, `SEXE`, `ADRESSE`, `TEL`, `MAIL`, `DateEntree`, `NIVEAUETUDES`) values (NULL, '$poste', '$statut', '$sitfam', '$service', '$nom',
 '$prenom', '$dateNaiss', '$sexe', '$adresse','$telephone', '$mail', '$dateEntree', '$nivEtudes');";
 
 $res_nouvEmpl = mysql_query($req_nouvEmpl); 
 return $res_nouvEmpl;
 }
 }
 
 
 function query_nouvEmpl($nom, $prenom, $idServ, $idPoste, $idStat){
$req_resultat = "Select idemp as ID,
		NOMEMP AS nom,
		PRENOMEMP AS prenom,
		DATENAISSANCE AS datenaiss,
		nomservice as service,
		nomposte as poste,
		typestat as statut
		from employe as E left join service as S on E.idservice=S.idservice
		left join poste as P on E.idposte=P.idposte
		left join statut as ST on E.idstat = ST.idstat
		where nomEmp = \"$nom\" and prenomemp = \"$prenom\" and 
		E.idstat = \"$idStat\" and E.idservice = \"$idServ\" and E.idposte = \"$idPoste\"";
	

$rep_resultat = mysql_query($req_resultat);

$tab_employ;
$employe = mysql_fetch_array($rep_resultat);
$tab_employ[0]['ID'] = $employe['ID'];
$tab_employ[0]['nom'] = $employe['nom'];
$tab_employ[0]['prenom'] = $employe['prenom'];
$tab_employ[0]['datenaiss'] = $employe['datenaiss'];
$tab_employ[0]['service'] = $employe['service'];
$tab_employ[0]['poste'] = $employe['poste'];
$tab_employ[0]['statut'] = $employe['statut'];

if(isset($tab_employ)) {
	return $tab_employ;
}

}
 
function query_insertComp($tab, $idEmp){
foreach ($tab as $elmt){
$req_compemp = "Insert into `pgrh`.`compemp` (`IDCOMP`, `IDEMP`) VALUES ('$elmt', '$idEmp')";
mysql_query($req_compemp);
}
}

function query_nouvComp($idComp, $competence){

$req_nvComp = "Insert into `pgrh`.`competence` (`IDCOMP`, `TYPECOMP`) VALUES ('$idComp', '$competence')";
$res_nvComp = mysql_query($req_nvComp);
}

function query_nouvServ($idServ, $nomServ){

$req_nvServ = "Insert into `pgrh`.`service` (`IDSERVICE`, `NOMSERVICE`) VALUES ('$idServ', '$nomServ')";
$res_nvServ = mysql_query($req_nvServ);
}

function query_nouvPoste($idPoste, $nomPoste){

$req_nvPoste = "Insert into `pgrh`.`poste` (`IDPOSTE`, `NOMPOSTE`) VALUES ('$idPoste', '$nomPoste')";
$res_nvPoste = mysql_query($req_nvPoste);
}



?>